package com.yunwushanhe.document.utils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.util.Random;

@Component
public class MailUtils {
    @Autowired             //这个爆红不用管他，是正常的
    private JavaMailSenderImpl mailSender;
    @Value("${spring.mail.username}")
    private String sender;

    /**
     *
     * @param to 发给谁
     * @param subject 标题
     * @param text 文件内容
     */
    public void sendSimpleEmail(String to,String subject,String text){
        //定制纯文本邮件发送信息
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom(sender);
        message.setTo(to);
        message.setSubject(subject);
        message.setText(text);
        mailSender.send(message);
    }

    public String createCode(){
        StringBuffer code=new StringBuffer();
        Random r = new Random();
        for (int i = 0; i < 5; i++) {
            int ran1 = r.nextInt(10);
            code.append(ran1);
        }
        return code.toString();
    }
}
